//
//  ZHLEEStatusFrame.swift
//  ZHLEE_Blog_Swift_Pro
//
//  Created by ZHLEEBlander on 15/10/16.
//  Copyright © 2015年 ZHLEEBlander. All rights reserved.
//

import UIKit

/// cell 中的magin值
private let Cell_Magin: CGFloat = 10

//名称的字体大小
let Cell_Status_Name_Font: CGFloat = 14
//创建时间的字体大小
let Cell_Status_CreateTime_Font: CGFloat = 10
//来源的字体大小
let Cell_Status_Scource_Font: CGFloat = 10
/// 内容的字体大小
let Cell_Status_Text_Font: CGFloat = 15

class ZHLEEStatusFrame: NSObject {
    
    var status: ZHLEEStatus?{
    
        didSet{
        
            setViewFrame()
        }
    }

    //cell高度
    var cellHeight : CGFloat = 0
// MARK: -
// MARK: -----------------原创微博--------------------------
    
    var originalViewF:CGRect?
    /// 头像Frame
    var headImageF:CGRect?
    /// 名称frame
    var nameLabelF:CGRect?
    
    var vipImageF:CGRect?
    /// 创造时间..
    var creatTimeF:CGRect?
    /// 来源
    var sourceLabelF:CGRect?
    /// 微博内容
    var contentTextF:CGRect?
    /// 原创微博的配图frame
    var originalPhotosF: CGRect?
    
// MARK: -
// MARK: -----------------转发微博--------------------------
    ///转发微博的整体view大小
    var retweetedViewF:CGRect?
    /// 转发微博的内容大小
    var retweetedTextF:CGRect?
    
    /// toolBar
    var cellToolBarViewF:CGRect?
    /// 转发微博配图
    var retweetPhotosF: CGRect?
    
// MARK: -
// MARK: -----------------方法--------------------------
    private func setViewFrame(){
    
        let status = self.status!
        
        /// 原创微博的整体view
        let originalViewX: CGFloat = 0
        let originalViewY = Cell_Magin
        let originalViewW = ScreenW
        
        //头像
        let headImageX = Cell_Magin
        let headImageY = Cell_Magin
        let headImageSize = CGSizeMake(35, 35)
        headImageF = CGRect(origin: CGPointMake(headImageX, headImageY), size: headImageSize)
        
        //名称
        let nameLabelX = CGRectGetMaxX(headImageF!) + Cell_Magin
        let nameLabelY = headImageY
        let nameLabelSize = status.user!.name!.size(UIFont.systemFontOfSize(Cell_Status_Name_Font))
        nameLabelF = CGRect(origin: CGPointMake(nameLabelX, nameLabelY), size: nameLabelSize)
        
        /**
        *  会员
        */
        if status.user!.isVip{
        
            let vipImageX = CGRectGetMaxX(nameLabelF!) + Cell_Magin
            let vipImageY = headImageY
            let vipImageSize = CGSizeMake(nameLabelSize.height, nameLabelSize.height)
            vipImageF = CGRect(origin: CGPointMake(vipImageX, vipImageY), size: vipImageSize)
            
        }
        
        //创建时间
        let creatTimeX = nameLabelX
        let creatTimeSize = status.createdAtStr.size(UIFont.systemFontOfSize(Cell_Status_CreateTime_Font))
        let creatTimeY = CGRectGetMaxY(headImageF!) - creatTimeSize.height
        creatTimeF = CGRect(origin: CGPointMake(creatTimeX, creatTimeY), size: creatTimeSize)
        
        //来源
        let sourceLabelX = CGRectGetMaxX(creatTimeF!) + Cell_Magin
        let sourceLabelY = creatTimeY
        let sourceLabelSize = status.source!.size(UIFont.systemFontOfSize(Cell_Status_Scource_Font))
        sourceLabelF = CGRect(origin: CGPointMake(sourceLabelX, sourceLabelY), size: sourceLabelSize)
        
        //原创微博的内容
        let contentTextX = Cell_Magin
        let contentTextY = CGRectGetMaxY(sourceLabelF!) + Cell_Magin
        let contentTextSize = status.text!.size(UIFont.systemFontOfSize(Cell_Status_Text_Font), constrainedToSize: CGSizeMake(ScreenW - Cell_Magin * 2, CGFloat(MAXFLOAT)))
        contentTextF = CGRect(origin: CGPointMake(contentTextX, contentTextY), size: contentTextSize)
        
//        原创微博内容的高
        var originalViewH = CGRectGetMaxY(contentTextF!) + Cell_Magin
        
        //原创微博的配图frame
        //判断,是否有配图
        if let originalPicUrls = status.pic_urls where originalPicUrls.count > 0{
        
            let originalPhotosX = Cell_Magin
            let originalPhotosY = originalViewH
            let originalPhotosSize = ZHLEEStatusPhotos.size(originalPicUrls.count)
            originalPhotosF = CGRect(origin: CGPointMake(originalPhotosX, originalPhotosY), size: originalPhotosSize)
            
            //有配图,重新设置原创微博的整体高度
            originalViewH = CGRectGetMaxY(originalPhotosF!) + Cell_Magin
        }
        
        //设置原创微博frame
        originalViewF = CGRectMake(originalViewX, originalViewY, originalViewW, originalViewH)
        
        //没有配图,没有转发微博的时候底部bar的y值
        var cellToolBarViewY = CGRectGetMaxY(originalViewF!)
        
        //判断,如果有转发微博
        if let reStatus = status.retweeted_status {
        
            let retweetedViewX: CGFloat = 0
            let retweetedViewY = cellToolBarViewY
            let retweetedViewW = ScreenW
            
            let retweetedTextX = Cell_Magin
            let retweetedTextY = Cell_Magin
            let retweetedTextSize = reStatus.text?.size(UIFont.systemFontOfSize(Cell_Status_Text_Font), constrainedToSize: CGSizeMake(ScreenW - Cell_Magin * 2, CGFloat(MAXFLOAT)))
            retweetedTextF = CGRect(origin: CGPointMake(retweetedTextX, retweetedTextY), size: retweetedTextSize!)
            
            //转发微博view的高度
            var retweetedViewH = CGRectGetMaxY(retweetedTextF!) + Cell_Magin
            
            if let retweetPicUrls = reStatus.pic_urls where retweetPicUrls.count > 0 {
            
                let retweetPhotosX = Cell_Magin
                let retweetPhotosY = retweetedViewH
                let retweetPhotosSize = ZHLEEStatusPhotos.size(retweetPicUrls.count)
                retweetPhotosF = CGRect(origin: CGPointMake(retweetPhotosX, retweetPhotosY), size: retweetPhotosSize)
                
                retweetedViewH = CGRectGetMaxY(retweetPhotosF!) + Cell_Magin
                
            }
            
            
            //转发微博的整体viewFrame
            retweetedViewF = CGRectMake(retweetedViewX, retweetedViewY, retweetedViewW, retweetedViewH)
            
            //有转发微博内容,重新设置底部toolBar的Y值
            cellToolBarViewY = CGRectGetMaxY(retweetedViewF!)
            
            
        }
        
        //cell底部toolBar
        let cellToolBarViewX:CGFloat = 0
        
        let cellToolBarViewSize = CGSizeMake(ScreenW, 35)
        cellToolBarViewF = CGRect(origin: CGPointMake(cellToolBarViewX, cellToolBarViewY), size: cellToolBarViewSize)
        
        cellHeight = CGRectGetMaxY(cellToolBarViewF!)
    }

}
